package ru.ifmo.thesis.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Shedule implements Cloneable {

	private final Map<Worker, List<Job>> shedule;

	private Shedule(Map<Worker, List<Job>> shedule) {
		this.shedule = shedule;
	}
	
	public Shedule() {
		this(new HashMap<Worker, List<Job>>());
	}
	
	public Shedule(Shedule copy) {
		this(new HashMap<Worker, List<Job>>(copy.shedule));
	}

	public void appendJob(Worker w, Job job) {
		if (!shedule.containsKey(w)) {
			shedule.put(w, new ArrayList<Job>());
		}
		
		shedule.get(w).add(job);
	}
	
	public Set<Worker> getWorkers() {
		return new HashSet<Worker>(shedule.keySet());
	}
	
	public List<Job> getPersonalShedule(Worker w) {
		return new ArrayList<Job>(shedule.get(w));
	}
	
	public void setPersonalShedule(Worker w, List<Job> schedule) {
		this.shedule.put(w, schedule);
	}
	
	@Override
	public Object clone() throws CloneNotSupportedException {
		Map<Worker, List<Job>> newShedule = new HashMap<Worker, List<Job>>(shedule);
		for (Map.Entry<Worker, List<Job>> e : newShedule.entrySet()) {
			e.setValue(new ArrayList<Job>(e.getValue()));
		}
		return new Shedule(newShedule);
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((shedule == null) ? 0 : shedule.hashCode());
		return result;
	}
}
